package com.deyou.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.deyou.Constants;
import com.deyou.R;
import com.deyou.adapter.AutoRankAdapter;
import com.deyou.adapter.CityPoiAdapter;
import com.deyou.adapter.CollectAdapter;
import com.deyou.adapter.HouseSearchResultAdapter;
import com.deyou.base.BaseActivity;
import com.deyou.callback.ServerApi;
import com.deyou.model.CityPoiBean;
import com.deyou.model.CollectionBean;
import com.deyou.model.HouseResourceAttrBean;
import com.deyou.model.LzyResponse;
import com.deyou.model.SimpleResponse;
import com.deyou.util.AESUtil;
import com.deyou.util.AndroidUtils;
import com.deyou.util.DateUtil;
import com.deyou.util.Logger;
import com.deyou.util.SPUtils;
import com.deyou.util.Urls;
import com.deyou.widget.DateRangeDecorator;
import com.deyou.widget.RangeSeekbar;
import com.example.zhouwei.library.CustomPopWindow;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.DefaultDayViewAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class HouseSearchResultListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.iv_left_back)
    ImageView ivLeftBack;
    @BindView(R.id.tv_mid_title)
    TextView tvMidTitle;
    @BindView(R.id.tv_date_selector)
    TextView tvDateSelector;
    @BindView(R.id.date_arrow)
    ImageView dateArrow;
    @BindView(R.id.date_selector_layout)
    LinearLayout dateSelectorLayout;
    @BindView(R.id.tv_location_selector)
    TextView tvLocationSelector;
    @BindView(R.id.location_arrow)
    ImageView locationArrow;
    @BindView(R.id.location_selector_layout)
    LinearLayout locationSelectorLayout;
    @BindView(R.id.tv_filters_selector)
    TextView tvFiltersSelector;
    @BindView(R.id.filters_arrow)
    ImageView filtersArrow;
    @BindView(R.id.filters_selector_layout)
    LinearLayout filtersSelectorLayout;
    @BindView(R.id.tv_sort_selector)
    TextView tvSortSelector;
    @BindView(R.id.sort_arrow)
    ImageView sortArrow;
    @BindView(R.id.sort_selector_layout)
    LinearLayout sortSelectorLayout;
    @BindView(R.id.house_recycler_view)
    RecyclerView houseRecyclerView;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private HouseSearchResultAdapter mAdapter;
    private boolean isRefresh = false;
    private int page = 1;
    private boolean isLoadMore = false;
    private int sortType = 0;
    private String priceRange = "0,20000";
    private String lonAndLat = "";
    private String resourceRegion = "";
    private String startDate = "";
    private String endDate = "";
    private String showStartDate = "";
    private String showEndDate = "";
    private String districtId = "";
    private String infrastructureName = "";
    private String amenitiesName = "";
    private String specialFacilitiesName = "";
    private String bedroomNum = "";
    private String maximumNum = "";
    private String resourceRentaltype = "";
    private String resourceStatus = "1,2,3,";

    private String selectCityPoiName = "";
    private String myCurrentCity = "";
    CalendarPickerView calenderView;
    List<Date> selectedDateList = new ArrayList<>();
    private CityPoiBean cityPoi;
    private HouseSearchResultAdapter.OnCollectItemClickListener collectItemClickListener = new HouseSearchResultAdapter.OnCollectItemClickListener() {
        @Override
        public void collectItemClick(HouseResourceAttrBean item) {
            if (item.collection == 1) { // 已收藏，进行取消收藏操作
                dealCollection(false, item);
            } else { // 未收藏，进行收藏操作
                dealCollection(true, item);
            }
        }
    };
    private CustomPopWindow calenderPopWin;
    private CustomPopWindow cityPoiPopWin;
    private CustomPopWindow moreFiltersPopWin;
    private CustomPopWindow autoRankPopWin;
    private List<String> stringList;
    private String keywords = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_search_result_list);
        ButterKnife.bind(this);
        initData();
        initView();
        getHouseSearchResult();
        getCityPoiData();
    }

    private void initData() {
        lonAndLat = getIntent().getExtras().getString("lonAndLat");
        resourceRegion = getIntent().getExtras().getString("regionCode");
        myCurrentCity = getIntent().getExtras().getString("myCurrentCity");
        keywords = getIntent().getExtras().getString("keywords");
    }

    private void initView() {
        tvMidTitle.setText("房源列表");

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);

        houseRecyclerView.setItemAnimator(new DefaultItemAnimator());
        houseRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mAdapter = new HouseSearchResultAdapter(this);
        houseRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(this);
        mAdapter.setCollectItemClick(collectItemClickListener);
    }

    @OnClick({R.id.iv_left_back, R.id.date_selector_layout, R.id.location_selector_layout, R.id.filters_selector_layout, R.id.sort_selector_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left_back:
                finish();
                break;
            case R.id.date_selector_layout:
                if (cityPoiPopWin != null)
                    cityPoiPopWin.dissmiss();
                showCalenderPopWin();
                break;
            case R.id.location_selector_layout:
                if (calenderPopWin != null)
                    calenderPopWin.dissmiss();
                showCityPoiPopWin();
                break;
            case R.id.filters_selector_layout:
                showMoreFiltersPopWin();
                break;
            case R.id.sort_selector_layout:
                showAutoRankPopWin();
                break;
        }
    }

    private void getCityPoiData() {
        JSONObject object = new JSONObject();
        try {
            object.put("regionId", resourceRegion);
            if (!TextUtils.isEmpty(myCurrentCity))
                object.put("regionName", myCurrentCity.replace("市", ""));
            else
                object.put("regionName", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String paramJson = object.toString();
        Type type = new TypeToken<LzyResponse<CityPoiBean>>() {
        }.getType();
        ServerApi.<LzyResponse<CityPoiBean>>getData(type, Urls.getCityPoi, "", AESUtil.encrypt(paramJson))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {

                    }
                })
                .map(new Function<LzyResponse<CityPoiBean>, CityPoiBean>() {
                    @Override
                    public CityPoiBean apply(@NonNull LzyResponse<CityPoiBean> listLzyResponse) throws Exception {
                        return listLzyResponse.data;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CityPoiBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull CityPoiBean cityPoiBeen) {
                        cityPoi = cityPoiBeen;
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getHouseSearchResult() {
        JSONObject object = new JSONObject();
        try {
            object.put("userId", SPUtils.getStringByKey(this, Constants.UserId));
            object.put("myuserId", SPUtils.getStringByKey(this, Constants.UserId));
            object.put("pageNo", page);
            object.put("pageSize", Constants.pageSize);
            object.put("resourceRegion", resourceRegion);
            object.put("startDate", TextUtils.isEmpty(startDate) ? DateUtil.getToday("yyyy-MM-dd") : startDate);
            object.put("endDate", TextUtils.isEmpty(endDate) ? DateUtil.getTomorry("yyyy-MM-dd") : endDate);
            object.put("priceRange", priceRange);
            object.put("location", lonAndLat);
            object.put("districtId", districtId);
            object.put("sortType", sortType);
            object.put("infrastructureName", infrastructureName);
            object.put("amenitiesName", amenitiesName);
            object.put("specialFacilitiesName", specialFacilitiesName);
            object.put("bedroomNum", bedroomNum);
            object.put("maximumNum", maximumNum);
            object.put("resourceRentaltype", resourceRentaltype);
            object.put("resourceStatus", resourceStatus);
            object.put("keywords", keywords);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String paramJson = object.toString();
        Logger.i(Logger.LOGTAG + "房源搜索入参", paramJson);
        Type type = new TypeToken<LzyResponse<HouseResourceAttrBean>>() {
        }.getType();
        ServerApi.<LzyResponse<HouseResourceAttrBean>>getData(type, Urls.getSearchHouseResult, "", AESUtil.encrypt(paramJson))//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        if (!isLoadMore && !isRefresh)
                            showLoading("获取房源中...");
                    }
                })//
                .map(new Function<LzyResponse<HouseResourceAttrBean>, HouseResourceAttrBean>() {
                    @Override
                    public HouseResourceAttrBean apply(@NonNull LzyResponse<HouseResourceAttrBean> response) throws Exception {
                        return response.data;
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())  //
                .subscribe(new Observer<HouseResourceAttrBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull HouseResourceAttrBean houseResourceBean) {
                        if (isRefresh)
                            setRefreshing(false);
                        else
                            dismissLoading();
                        if (null != houseResourceBean) {
                            if (null != houseResourceBean.content && houseResourceBean.content.size() > 0) {
                                if (houseResourceBean.first)
                                    mAdapter.setNewData(houseResourceBean.content);
                                else
                                    mAdapter.addData(houseResourceBean.content);
                            } else {
                                if (page == 1) {
                                    mAdapter.setNewData(null);
                                }
                            }
                            mAdapter.setEnableLoadMore(!houseResourceBean.last);
                        } else {
                            if (page == 1) {
                                mAdapter.setNewData(null);
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();            //请求失败
                        if (isRefresh)
                            setRefreshing(false);
                        else
                            dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }


    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getHouseSearchResult();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle = new Bundle();
        if (null != mAdapter && mAdapter.getData() != null && mAdapter.getData().size() > 0 && mAdapter.getData().get(position) != null) {
            bundle.putString("resourceCode", mAdapter.getData().get(position).resourceId);
            AndroidUtils.startNextActivity(HouseSearchResultListActivity.this, HouseResourceDetailActivity.class, bundle, false);
        }
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        getHouseSearchResult();
    }

    private void showCalenderPopWin() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_house_calender_select, null);
        handleCalenderView(contentView);
        calenderPopWin = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                .create();
        calenderPopWin.showAsDropDown(dateSelectorLayout);
    }

    private void handleCalenderView(View popView) {
        if (null != selectedDateList && selectedDateList.size() > 0)
            selectedDateList.clear();
        if (TextUtils.isEmpty(startDate))
            selectedDateList.add(DateUtil.string2Date10(DateUtil.getToday("yyyy-MM-dd")));
        else
            selectedDateList.add(DateUtil.string2Date10(startDate));
        if (!TextUtils.isEmpty(endDate))
            selectedDateList.add(DateUtil.string2Date10(endDate));
        else
            selectedDateList.add(DateUtil.string2Date10(DateUtil.getTomorry("yyyy-MM-dd")));
        calenderView = (CalendarPickerView) popView.findViewById(R.id.calender_view);
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 1);

        calenderView.setCustomDayView(new DefaultDayViewAdapter());
        calenderView.setDecorators(Arrays.<CalendarCellDecorator>asList(new DateRangeDecorator(this)));
        calenderView.init(new Date(), nextYear.getTime()) //
                .inMode(CalendarPickerView.SelectionMode.RANGE)
                .withSelectedDates(selectedDateList);

        calenderView.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Logger.i(Logger.LOGTAG + "选择日期", DateUtil.date2String10(date));
            }

            @Override
            public void onDateUnselected(Date date) {
                Logger.i(Logger.LOGTAG + "取消选择日期", DateUtil.date2String10(date));
            }
        });

        popView.findViewById(R.id.btn_select_calender).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Date> dateList = calenderView.getSelectedDates();
                if (dateList.size() > 0 && dateList.size() < 2) {
                    showToast("请选择入住日期跟离店日期查询");
                } else if (dateList.size() >= 2) {
                    startDate = DateUtil.date2String10(dateList.get(0));
                    endDate = DateUtil.date2String10(dateList.get(dateList.size() - 1));
                    showStartDate = DateUtil.date2StringByFormay(dateList.get(0), "MM.dd");
                    showEndDate = DateUtil.date2StringByFormay(dateList.get(dateList.size() - 1), "MM.dd");
                    handler.sendEmptyMessage(10010);
                } else {
                    startDate = "";
                    endDate = "";
                }
            }
        });
    }

    private void showCityPoiPopWin() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.city_poi_layout, null);
        handleCityPoiView(contentView);
        cityPoiPopWin = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                .create();
        cityPoiPopWin.showAsDropDown(locationSelectorLayout);
    }

    private void handleCityPoiView(View contentView) {
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.poi_data_list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        final CityPoiAdapter adapter = new CityPoiAdapter(this);
        if (null != cityPoi)
            adapter.setNewData(cityPoi.region);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseAdapter, View view, int position) {
                CityPoiBean bean = adapter.getData().get(position);
                if (null != bean) {
                    districtId = bean.poiInfoId;
                    lonAndLat = bean.location;
                    selectCityPoiName = bean.name;
                    adapter.setSelectedId(bean.poiInfoId);
                }
            }
        });

        final TextView adminArea = (TextView) contentView.findViewById(R.id.tv_admin_area);
        final TextView airPort = (TextView) contentView.findViewById(R.id.tv_airport);
        final TextView school = (TextView) contentView.findViewById(R.id.tv_school);
        final TextView businessZone = (TextView) contentView.findViewById(R.id.tv_business_zone);
        final TextView trainStation = (TextView) contentView.findViewById(R.id.tv_train_station);
        final TextView hospital = (TextView) contentView.findViewById(R.id.tv_hospital);
        final TextView cancel = (TextView) contentView.findViewById(R.id.btn_cancel);
        final TextView confirm = (TextView) contentView.findViewById(R.id.btn_confirm);
        final TextView tvSearchPoi = (TextView) contentView.findViewById(R.id.tv_search_poi);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adminArea.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                airPort.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                school.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                businessZone.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                trainStation.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                hospital.setBackgroundColor(getResources().getColor(R.color.color_EBEBEB));
                switch (v.getId()) {
                    case R.id.tv_admin_area:
                        adminArea.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.region);
                        break;
                    case R.id.tv_airport:
                        airPort.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.airport);
                        break;
                    case R.id.tv_school:
                        school.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.school);
                        break;
                    case R.id.tv_business_zone:
                        businessZone.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.businesszone);
                        break;
                    case R.id.tv_train_station:
                        trainStation.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.trainstation);
                        break;
                    case R.id.tv_hospital:
                        hospital.setBackgroundColor(getResources().getColor(R.color.white));
                        if (null != cityPoi)
                            adapter.setNewData(cityPoi.hospital);
                        break;
                }
            }
        };

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cityPoiPopWin != null)
                    cityPoiPopWin.dissmiss();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(10011);
            }
        });
        tvSearchPoi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AndroidUtils.startNextActivity(HouseSearchResultListActivity.this, HouseSearchListActivity.class, null, false);
                if (null != cityPoiPopWin)
                    cityPoiPopWin.dissmiss();
            }
        });

        adminArea.setOnClickListener(listener);
        airPort.setOnClickListener(listener);
        school.setOnClickListener(listener);
        businessZone.setOnClickListener(listener);
        trainStation.setOnClickListener(listener);
        hospital.setOnClickListener(listener);
    }

    private void showMoreFiltersPopWin() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.more_selection_layout, null);
        handleMoreFiltersView(contentView);
        moreFiltersPopWin = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
                .create();
        moreFiltersPopWin.showAsDropDown(filtersSelectorLayout);
    }

    private void handleMoreFiltersView(View contentView) {
        final RangeSeekbar seekBar = (RangeSeekbar) contentView.findViewById(R.id.seek_bar);
        final TextView cancel = (TextView) contentView.findViewById(R.id.btn_reset);
        final TextView confirm = (TextView) contentView.findViewById(R.id.btn_confirm);
        final String[] priceArray = getResources().getStringArray(R.array.markArray);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                priceRange = priceArray[seekBar.getLeftCursorIndex()] + "," + priceArray[seekBar.getRightCursorIndex()];
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moreFiltersPopWin.dissmiss();
            }
        });
    }

    private void showAutoRankPopWin() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.auto_rank_layout, null);
        handleAutoRankView(contentView);
        autoRankPopWin = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .setBgDarkAlpha(0.7f)
                .enableBackgroundDark(true)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create();
        autoRankPopWin.showAsDropDown(sortSelectorLayout);
    }

    private void handleAutoRankView(View contentView) {
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        final AutoRankAdapter adapter = new AutoRankAdapter(this);
        stringList = new ArrayList<>();
        stringList.add("推荐排序");
        stringList.add("好评排序");
        stringList.add("价格 低-高");
        stringList.add("价格 高-低");
        adapter.setSelectedId(sortType);
        adapter.setConditionList(stringList);
        recyclerView.setAdapter(adapter);

        AutoRankAdapter.OnItemClickListener listener = new AutoRankAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                adapter.setSelectedId(position);
                sortType = position;
                handler.sendEmptyMessage(10013);
            }
        };
        adapter.setOnItemClick(listener);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1: // 没有数据
                    if (page == 1) {
                    } else {
                    }
                    break;
                case 2:
                    break;
                case 10010:
                    if (null != calenderPopWin)
                        calenderPopWin.dissmiss();
                    tvDateSelector.setText(showStartDate + "-" + showEndDate);
                    tvDateSelector.setTextColor(getResources().getColor(R.color.color_FF832D));
                    onRefresh();
                    break;
                case 10011:
                    if (null != cityPoiPopWin)
                        cityPoiPopWin.dissmiss();
                    tvLocationSelector.setText(selectCityPoiName);
                    tvLocationSelector.setTextColor(getResources().getColor(R.color.color_FF832D));
                    onRefresh();
                    break;
                case 10013:
                    if (null != autoRankPopWin)
                        autoRankPopWin.dissmiss();
                    tvSortSelector.setText(stringList.get(sortType));
                    tvSortSelector.setTextColor(getResources().getColor(R.color.color_FF832D));
                    onRefresh();
                    break;

            }
        }
    };

    private void dealCollection(final boolean isCollection, final HouseResourceAttrBean houseResource) {
        JSONObject object = new JSONObject();
        try {
            if (isCollection) {
                object.put("resourceId", houseResource.resourceId);
                object.put("userId", SPUtils.getStringByKey(HouseSearchResultListActivity.this, Constants.UserId));
                object.put("resourcePic", houseResource.resourceMainPic);
                object.put("resourceName", houseResource.resourceName);
                object.put("resourceRentaltypename", houseResource.resourceRentaltypeName);
                object.put("resourceRegionname", houseResource.resourceCityName);
                object.put("evaluateStar", houseResource.evaluateStar);
                object.put("evaluateNum", houseResource.evaluateNum);
                object.put("userPic", houseResource.userPic);
                object.put("resourcePrice", houseResource.resourcePrice);
            } else {
                object.put("collectionId", houseResource.collectionId);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String paramJson = object.toString();
        ServerApi.getString(isCollection ? Urls.addCollection : Urls.deleteCollection, "", AESUtil.encrypt(paramJson))//
                .subscribeOn(Schedulers.io())//
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        showLoading(isCollection ? "添加收藏请求中..." : "取消收藏请求中...");
                    }
                })//
                .observeOn(AndroidSchedulers.mainThread())  //
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        SimpleResponse resp = new Gson().fromJson(s, SimpleResponse.class);
                        List<HouseResourceAttrBean> houseList = new ArrayList<HouseResourceAttrBean>();
                        if (null != resp && resp.code == 1) {
                            for (HouseResourceAttrBean house : mAdapter.getData()) {
                                if (house.resourceId.equals(houseResource.resourceId)) {
                                    if (house.collection == 1) {
                                        house.collection = 0;
                                    } else {
                                        house.collection = 1;
                                    }
                                }
                                houseList.add(house);
                            }
                            mAdapter.setNewData(houseList);
                            Toast.makeText(HouseSearchResultListActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(HouseSearchResultListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();            //请求失败
                        dismissLoading();
                        Toast.makeText(HouseSearchResultListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }
}
